package net.butfly.bus.client;

import net.butfly.bus.router.Router;

class DefaultRouter implements Router {
	private ClientConfig config;

	public DefaultRouter(ClientConfig config) {
		this.config = config;
	}

	@Override
	public String route(String tx, String version) {
		// param version is ignored at client.
		return this.route(tx);
	}

	@Override
	public String route(String tx) {
		String[] nodes = this.config.getNodeIdsByTxMatching(tx);
		if (null == nodes)
			return null;
		switch (nodes.length) {
		case 0:
			return null;
		case 1:
			return nodes[0];
		default:
			return nodes[(int) Math.random() * nodes.length];
		}
	}
}
